package org.djw.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.djw.basic.exception.BusinessException;
import org.djw.basic.util.AjaxResult;
import org.djw.basic.util.PageList;
import org.djw.user.domain.User;
import org.djw.user.dto.UserDto;
import org.djw.user.query.UserQuery;
import org.djw.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理接口
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {
    @Autowired
    private IUserService userService;


    /**
     * 手机号注册
     * @param userDto
     * @return
     */
    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }

    }
    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/list")
    public List<User> queryAll(){
        return userService.queryAll();
    }

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @PostMapping("/page")
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryPage(query);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/batch")
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            userService.batch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }

    /**
     * 新增或者修改
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId() == null){
                userService.add(user);
            } else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }
}
